<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="耳标号" prop="earSn">
        <el-input
          v-model="queryParams.earSn"
          placeholder="请输入耳标号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="购入时间" prop="buyTime">
        <el-date-picker clearable
          v-model="queryParams.buyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择购入时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="来源类型" prop="sourceType">
        <el-select
          v-model="queryParams.sourceType"
          placeholder="请选择来源类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.source_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.farm_cattle_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="毛色" prop="coatColor">
        <el-input
          v-model="queryParams.coatColor"
          placeholder="请输入毛色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="出生日期" prop="birthday">
        <el-date-picker clearable
          v-model="queryParams.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="牛只类别" prop="cattleType">
        <el-select
          v-model="queryParams.cattleType"
          placeholder="请选择牛只类别"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cattle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="牛只等级" prop="grade">
        <el-select
          v-model="queryParams.grade"
          placeholder="请选择牛只等级"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cattle_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="牛只状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择牛只状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cattle_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="繁育状态" prop="breedStatus">
        <el-select
          v-model="queryParams.breedStatus"
          placeholder="请选择繁育状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.breed_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['farm:cattle:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['farm:cattle:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['farm:cattle:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['farm:cattle:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="cattleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="牛只编号" align="center" prop="id" />
      <el-table-column label="耳标号" align="center" prop="earSn" />
      <!-- <el-table-column
        label="购入时间"
        align="center"
        prop="buyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="来源类型" align="center" prop="sourceType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.source_type"
            :value="scope.row.sourceType"
          />
        </template>
      </el-table-column>
      <el-table-column label="品种ID" align="center" prop="varietyId" />
      <!-- <el-table-column label="母ID" align="center" prop="motherId" />
      <el-table-column label="父ID" align="center" prop="fatherId" /> -->
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.farm_cattle_sex"
            :value="scope.row.sex"
          />
        </template>
      </el-table-column>
      <el-table-column label="毛色" align="center" prop="coatColor" />
      <el-table-column
        label="出生日期"
        align="center"
        prop="birthday"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="牛只类别" align="center" prop="cattleType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cattle_type"
            :value="scope.row.cattleType"
          />
        </template>
      </el-table-column>
      <el-table-column label="牛只等级" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cattle_grade"
            :value="scope.row.grade"
          />
        </template>
      </el-table-column>
      <el-table-column label="牛只状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cattle_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="繁育状态" align="center" prop="breedStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.breed_status"
            :value="scope.row.breedStatus"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="正面照" align="center" prop="facePhoto" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['farm:cattle:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['farm:cattle:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改牛只对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="耳标号" prop="earSn">
          <el-input v-model="form.earSn" placeholder="请输入耳标号" />
        </el-form-item>
        <el-form-item label="购入时间" prop="buyTime" v-if="false">
          <el-date-picker
            clearable
            v-model="form.buyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购入时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="来源类型" prop="sourceType">
          <el-select v-model="form.sourceType" placeholder="请选择来源类型">
            <el-option
              v-for="dict in dict.type.source_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="母ID" prop="motherId">
          <el-input v-model="form.motherId" placeholder="请输入母ID" />
        </el-form-item>
        <el-form-item label="父ID" prop="fatherId">
          <el-input v-model="form.fatherId" placeholder="请输入父ID" />
        </el-form-item> -->
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.farm_cattle_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="毛色" prop="coatColor">
          <el-input v-model="form.coatColor" placeholder="请输入毛色" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker
            clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="品种" prop="varietyId">
          <el-select v-model="form.varietyId" placeholder="请选择牛只品种">
            <el-option
              v-for="dict in varietyList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="牛只类别" prop="cattleType">
          <el-select v-model="form.cattleType" placeholder="请选择牛只类别">
            <el-option
              v-for="dict in dict.type.cattle_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="牛只等级" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择牛只等级">
            <el-option
              v-for="dict in dict.type.cattle_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="牛只状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择牛只状态">
            <el-option
              v-for="dict in dict.type.cattle_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="繁育状态" prop="breedStatus">
          <el-select v-model="form.breedStatus" placeholder="请选择繁育状态">
            <el-option
              v-for="dict in dict.type.breed_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="正面照" prop="facePhoto">
          <el-input v-model="form.facePhoto" placeholder="请输入正面照" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCattle,
  getCattle,
  delCattle,
  addCattle,
  updateCattle,
} from "@/api/farm/cattle";

import { allVarieties } from "@/api/farm/varieties";

export default {
  name: "Cattle",
  dicts: [
    "farm_cattle_sex",
    "source_type",
    "cattle_grade",
    "cattle_type",
    "cattle_status",
    "breed_status",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 牛只表格数据
      cattleList: [],
      //品种表
      varietyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        earSn: null,
        buyTime: null,
        sourceType: null,
        varietyId: null,
        motherId: null,
        fatherId: null,
        sex: null,
        coatColor: null,
        birthday: null,
        cattleType: null,
        grade: null,
        status: null,
        breedStatus: null,
        facePhoto: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        earSn: [{ required: true, message: "耳标号不能为空", trigger: "blur" }],
        sourceType: [
          {
            required: true,
            message: "来源类型-字典不能为空",
            trigger: "change",
          },
        ],
        varietyId: [
          { required: true, message: "品种ID不能为空", trigger: "change" },
        ],
        sex: [
          { required: true, message: "性别-字典不能为空", trigger: "change" },
        ],
        birthday: [
          { required: true, message: "出生日期不能为空", trigger: "blur" },
        ],
        grade: [
          {
            required: true,
            message: "牛只等级-字典不能为空",
            trigger: "change",
          },
        ],
        status: [
          {
            required: true,
            message: "牛只状态-字典不能为空",
            trigger: "change",
          },
        ],
        breedStatus: [
          {
            required: true,
            message: "繁育状态-字典不能为空",
            trigger: "change",
          },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getVarieties();
  },
  methods: {
    /** 查询牛只列表 */
    getList() {
      this.loading = true;
      listCattle(this.queryParams).then((response) => {
        this.cattleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        earSn: null,
        buyTime: null,
        sourceType: null,
        varietyId: null,
        motherId: null,
        fatherId: null,
        sex: null,
        coatColor: null,
        birthday: null,
        cattleType: null,
        grade: null,
        status: null,
        breedStatus: null,
        facePhoto: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getVarieties() {
      allVarieties().then((response) => {
        this.varietyList = response.data;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加牛只";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCattle(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改牛只";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCattle(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCattle(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除牛只编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCattle(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "farm/cattle/export",
        {
          ...this.queryParams,
        },
        `cattle_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
